import MyTouch from "./MyTouch";
import MyWeapon from "./MyWeapon";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

	@property(cc.Node)
	nodeWeapon: cc.Node = null;

	@property(cc.Node)
	nodeTouch: cc.Node = null;


    _weapon:MyWeapon=null;

	onLoad() {
        this._weapon=this.nodeWeapon.getComponent(MyWeapon);
	}

	start() {
		this.node.on(
			cc.Node.EventType.TOUCH_START,
			this.onTouchStart,
			this
		);

		this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);

		this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
		this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
	}

	// update (dt) {}

	onTouchStart(e: cc.Event.EventTouch) {
		// 方向
		this.nodeTouch.active = true;
		let pos = this.nodeTouch.parent.convertToNodeSpaceAR(e.getLocation());
		this.nodeTouch.getComponent(MyTouch).setStartPos(pos);
		// 武器
		this._weapon.prepare();
	}

	onTouchMove(e: cc.Event.EventTouch) {
		// 方向
		let pos = this.nodeTouch.parent.convertToNodeSpaceAR(e.getLocation());
		let jsTouch = this.nodeTouch.getComponent(MyTouch);
		jsTouch.setEndPos(pos);

		// 武器
		cc.log(jsTouch.getForce());
		this._weapon.setForce(jsTouch.getForce());
		this.nodeWeapon.rotation = this.nodeTouch.rotation;
	}

	onTouchEnd(e: cc.Event.EventTouch) {
		// 方向
		this.nodeTouch.active = false;
		// 武器
		this._weapon.fire();
	}
}
